#include "client.h"
pthread_mutex_t db_mutex;
int exitPipe[2]; // 主进程通知子进程退出管道
void sigFunc_user(int signum)
{
    printf("signum = %d\n", signum);
    write(exitPipe[1], "1", 1);
    printf("Parent process is going to die!\n");
}
void sigFunc_c(int signum)
{
    printf("signum = %d\n", signum);
    write(exitPipe[1], "1", 1);
    printf("Parent process is going to die!\n");
}
int main(int argc, char *argv[])
{
    // ./main  ip port
    ARGS_CHECK(3, argc);
    int ret = pipe(exitPipe);
    ERROR_CHECK(-1, ret, "pipe");
    int cnd = tcp_cnd_init(argv[1], atoi(argv[2]));
    if (-1 == cnd)
    {
        perror("tcp_cnd_init");
        puts("连结服务器失败，进入离线模式");
    }
    else
    {
        puts("服务器连接成功");
        char buf[1024] = {0};
        userSys(cnd);
        
        system("clear");
        printf("****************************************\n");
        printf("********欢迎使用分布式图片处理系统********\n");
        printf("*************ctrl + c键退出**************\n");
        printf("****1、上传任务处理请求 ******************\n");
        printf("*********请把任务图片放入srcImg目录*******\n");
        
        printf("****************************************\n");
        if (fork() != 0) 
        { 
            close(exitPipe[0]);
            signal(SIGUSR1, sigFunc_user);
            signal(SIGINT, sigFunc_c);
            wait(NULL); 
            exit(0);
        }
        close(exitPipe[1]);
        int worker_num = 5;
        threadPool thread_pool;                   // tid,taskqueue 初始化
        threadPoolInit(&thread_pool, worker_num); // 初始化线程池内存
        pthread_mutex_init(&db_mutex, NULL);
        makeWorker(&thread_pool); // 创建若干个子线程；
        // epoll
        int epfd = epoll_create(1);
        epollAdd(exitPipe[0], epfd);
        epollAdd(cnd, epfd);
        epollAdd(STDIN_FILENO, epfd);
        struct epoll_event readyArr[5];
        while (1)
        {
            int ready_num = epoll_wait(epfd, readyArr, 5, -1);
            ERROR_CHECK(ready_num, -1, "epoll_wait");
            printf("epoll_wait returns\n");
            for (size_t i = 0; i < ready_num; i++)
            {
                int ready_fd = readyArr[i].data.fd;
                if (ready_fd == cnd)
                {
                    recv(cnd, buf, 1024, 0);
                    if (0 == strcmp(buf, TASK_DOWN))
                    { // 加锁
                        pthread_mutex_lock(&thread_pool.taskQueue.mutex);
                        taskEnqueue(&thread_pool.taskQueue, cnd); // 生产一个任务
                        printf("New task!-->%d\n",ready_fd);
                        pthread_cond_signal(&thread_pool.taskQueue.cond); // 通知处于唤醒队列的线程
                        pthread_mutex_unlock(&thread_pool.taskQueue.mutex);
                    }
                }
                else if (ready_fd == exitPipe[0])
                {
                    printf("child process,threadPool is going to die\n");
                    thread_pool.exitFlag = 1;
                    pthread_cond_broadcast(&thread_pool.taskQueue.cond);
                    for (size_t i = 0; i < worker_num; i++)
                    {
                        pthread_join(thread_pool.pTid[i], NULL);
                    }
                    pthread_exit(NULL);
                }
                else if (ready_fd == STDIN_FILENO)
                {
                    int cmd_user = 0;
                    cmd_user = fgetc(stdin) -'0';
                    getchar();
                    switch (cmd_user)
                    {
                    case 1:
                        puts("up1");
                        uploadingTask(cnd);
                        break;
                    // case 2:
                    //     uptsk(cnd,taskQueue);
                    //     break;
                    default:  
                        break; 
                    }
                }
            }
        }
    }
}